package com.ruoyi.sale.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sale.domain.SaleConfigStyle;
import com.ruoyi.sale.service.ISaleConfigStyleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 样式配置Controller
 *
 * @author szm
 * @date 2025-11-15
 */
@Api(tags = "样式配置接口")
@RestController
@RequestMapping("/sale/style")
public class SaleConfigStyleController extends BaseController {
    @Autowired
    private ISaleConfigStyleService saleConfigStyleService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询样式配置列表
     */
    @ApiOperation("查询样式配置列表")
    @GetMapping("/list")
    public TableDataInfo list(SaleConfigStyle entity, HttpServletRequest req) {
        startPage();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<SaleConfigStyle> list = saleConfigStyleService.list(wrapper);
        for (SaleConfigStyle style : list) {
            SysDept dept = sysDeptService.selectDeptById(style.getDeptId());
            if (dept != null) {
                style.setDeptName(dept.getDeptName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出样式配置列表
     */
    @ApiOperation("导出样式配置列表")
    @Log(title = "样式配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleConfigStyle entity) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<SaleConfigStyle> list = saleConfigStyleService.list(wrapper);
        ExcelUtil<SaleConfigStyle> util = new ExcelUtil<SaleConfigStyle>(SaleConfigStyle.class);
        util.exportExcel(response, list, "样式配置数据");
    }

    /**
     * 获取样式配置详细信息
     */
    @ApiOperation("获取样式配置详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") String id) {
        return success(saleConfigStyleService.getById(id));
    }

    /**
     * 新增样式配置
     */
    @ApiOperation("新增/修改样式配置")
    @Log(title = "样式配置", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SaleConfigStyle entity) {
        return toAjax(saleConfigStyleService.saveOrUpdate(entity));
    }


    /**
     * 删除样式配置
     */
    @ApiOperation("删除样式配置")
    @Log(title = "样式配置", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        return toAjax(saleConfigStyleService.removeById(ids));
    }
}
